package com.hspedu.homework;

/**
 * @author LiuJinTao
 * @className HomeWork02
 * @date 2024/06/23 22:35
 * @description TODO
 */
public class HomeWork02 {
    public static void main(String[] args) {
        Card card = new Card();
        Thread thread1 = new Thread(card);
        thread1.setName("用户一");
        Thread thread2 = new Thread(card);
        thread2.setName("用户二");
        thread1.start();
        thread2.start();
    }
}

class Card implements Runnable {
    private double balance = 10000;


    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (balance < 1000) {
                    System.out.println("余额不足");
                    break;
                }
                balance -= 1000;
                System.out.println(Thread.currentThread().getName() + "取剩余" + balance + "元");
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }
}